﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessTier.Facade.Interface;
using Domain;
using BusinessTier.DataContext;

namespace BusinessTier.Facade.Implementation
{
    public class OrderService : IOrderService
    {
        public IEnumerable<Customer> GetAllCustomers()
        {
            using (var ctx = new SimplyData())
            {
                return ctx.Customers.OrderBy(c => c.CreateDate).ToList();
            }
        }

        public void SaveCustomer(Customer customer)
        {
            using (var ctx = new SimplyData())
            {
                if (customer.CustomerId > 0)
                {
                    ctx.Customers.Attach(customer);
                    
                }
                else
                {
                    customer.CreateDate = DateTime.Now;
                    ctx.Customers.Add(customer);
                }
                
                ctx.SaveChanges();
            }
        }


        public Customer GetCustomerById(int id)
        {
            using (var ctx = new SimplyData())
            {
                return ctx.Customers.Find(id);
            }
        }
    }
}
